<?php
    session_start();
    include "../../function/functionPHP.php";
    
    $reportMember=$_SESSION["report"]["member"];
    $coopID=$_POST["coopID"];
    
    $mysqli=connect();
    $keyword=$_POST["keyword"];
    
    $temp="";
    
    if(is_array($reportMember)){
        foreach ($reportMember as $key => $value) {
            $temp.="'$key',";
        }
    }
    
    $temp=  substr($temp, 0,  strlen($temp)-1);
    
    if($temp==""){
        $temp="''";
    }
    
    if($coopID==""){
        $query="
            select  *
            from    member
            where   (
                        firstName like '%".$keyword."%' or
                        lastName like '%".$keyword."%'
                    ) and
                    memID not in (
                        ".$temp."
                    ) and
                    memID in (
                        select  memID
                        from    member_farm
                        where   farmID in (
                                    select  farmID
                                    from    member_farm_crop
                                )
                    )
            ";
    }else{
        $query="
            select  *
            from    member
            where   (
                        firstName like '%".$keyword."%' or
                        lastName like '%".$keyword."%'
                    ) and
                    memID not in (
                        ".$temp."
                    ) and 
                    coopID='".$coopID."' and
                    memID in (
                        select  memID
                        from    member_farm
                        where   farmID in (
                                    select  farmID
                                    from    member_farm_crop
                                )
                    )
            ";
    }
    $result=  $mysqli->query($query);
    while($row=$result->fetch_array(MYSQLI_ASSOC)){
        ?>
<div class="hover" style="padding: 5px;cursor: pointer;"
    onclick="setMember('<?=$row["memID"]?>')"
    ><?php echo $row["firstName"]." ".$row["lastName"];?>&nbsp;<span class='glyphicon glyphicon-arrow-right' style="float: right;"></span></div>
            <?php
    }

    